//
//  HNRecreationViewController.m
//  HappyNews
//
//  Created by lanou on 16/1/16.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "HNRecreationViewController.h"

#import "HNRequestManager.h"

#import "HNChoicenessModel.h"

#import "KrVideoPlayerController.h"

#import "HNChoicenessModel.h"

#import "HNRecreationTableViewCell.h"

@interface HNRecreationViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *headerArr;

@property (nonatomic, strong) KrVideoPlayerController *videoController;

@property (nonatomic, strong) UIButton *backButton;

@property (nonatomic, strong) UITableView *HNRecreationTabelView;

@property (nonatomic, assign) NSInteger index;

@end

@implementation HNRecreationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    _headerArr = [NSMutableArray array];
    
    self.navigationController.navigationBarHidden = YES;
    
    self.tabBarController.tabBar.hidden = YES;
    
    [self setHNRecreationData];
    
    [self addHNRecreatTabelView];
    
}

- (void)setHNRecreationData
{
    NSString *url = [NSString stringWithFormat:@"http://vcsp.ifeng.com/vcsp/appData/getGuidRelativeVideoList.do?pageSize=20&guid=%@",_model.guid];
    
    NSLog(@"%@",url);
    
    [HNRequestManager requestWithUrlString:url parDic:nil method:GET finish:^(NSData *data) {
        
        NSDictionary *HNRecreationDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *HNRecreationArr = HNRecreationDic[@"guidRelativeVideoInfo"];
        
        for (NSMutableDictionary *HNTwoDic in HNRecreationArr) {
            HNChoicenessModel *model = [[HNChoicenessModel alloc]init];
            [model setValuesForKeysWithDictionary:HNTwoDic];
            [_headerArr addObject:model];
        }
        _index = 0;
        
        HNChoicenessModel *model = _headerArr[_index];
        
        [self addVideoPlayerWithURL:[NSURL URLWithString:model.mediaUrl]];
        
        [self addButton];
        
        [_HNRecreationTabelView reloadData];
        
    } error:^(NSError *error) {
        
    }];
    
    
    
}

- (void)addButton
{
    _backButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    _backButton.frame = CGRectMake(10, 10, 30, 30);
    
    [_backButton addTarget:self action:@selector(ButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [_backButton setBackgroundImage:[UIImage imageNamed:@"iconfont-unie642"] forState:(UIControlStateNormal)];
    [self.videoController.view addSubview:_backButton];
    
}

- (void)ButtonAction:(UIButton *)button
{
    __weak typeof(self)weakSelf = self;
    [self.videoController setDimissCompleteBlock:^{
        weakSelf.videoController = nil;
    }];

    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    
}

// 视频播放
- (void)addVideoPlayerWithURL:(NSURL *)url{
    if (!self.videoController) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        self.videoController = [[KrVideoPlayerController alloc] initWithFrame:CGRectMake(0, 20, width, width*(9.0/16.0))];
        __weak typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
        }];
        [self.videoController setWillBackOrientationPortrait:^{
            [weakSelf toolbarHidden:NO];
        }];
        [self.videoController setWillChangeToFullscreenMode:^{
            [weakSelf toolbarHidden:YES];
        }];
        [self.view addSubview:self.videoController.view];
    }
    self.videoController.contentURL = url;
    
}
//隐藏navigation tabbar 电池栏
- (void)toolbarHidden:(BOOL)Bool{
    self.navigationController.navigationBar.hidden = Bool;
    self.tabBarController.tabBar.hidden = Bool;
    [[UIApplication sharedApplication] setStatusBarHidden:Bool withAnimation:UIStatusBarAnimationFade];
}

- (void)addHNRecreatTabelView
{
    _HNRecreationTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, kScreenWidth * (9.0 / 16.0), kScreenWidth, kScreenHeight - 183) style:(UITableViewStylePlain)];
    
    _HNRecreationTabelView.delegate = self;
    
    _HNRecreationTabelView.dataSource = self;
    
    _HNRecreationTabelView.rowHeight = kScreenHeight / 6;
    
    
    [_HNRecreationTabelView registerClass:[HNRecreationTableViewCell class] forCellReuseIdentifier:@"HNRecreationTableViewCell"];
    
    [self.view addSubview:_HNRecreationTabelView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _headerArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNRecreationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HNRecreationTableViewCell" forIndexPath:indexPath];
    
    HNChoicenessModel *model = _headerArr[indexPath.row];
    
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNChoicenessModel *model = _headerArr[indexPath.row];
    
    [self addVideoPlayerWithURL:[NSURL URLWithString:model.mediaUrl]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
